package com.nettyRpc.consumer;

import com.nettyRpc.api.DubboRequest;
import io.netty.channel.*;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 *
 */
public class DubboInvoke implements InvocationHandler {

    private Class clazz;

    private Channel channel;

    public DubboInvoke(Class clazz, Channel channel) {
        this.clazz = clazz;
        this.channel = channel;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) {
        //代理对象-通道写数据
        DubboRequest request=new DubboRequest();
        request.setId(Math.round(100));
        request.setClassName(clazz.getName());
        request.setMethod(method.getName());
        request.setArgs(args);

        this.channel.writeAndFlush(request);
        //事件监听线程-
        return new DubboFuture(request.getId()).get().getData();

    }
}
